<template>
  <sn-three-col-space :side-content="5" :content="14">
    <div slot="spaceContent" class="login">
      <el-link type="danger" :underline="false" class="login-title" @click="toHome">
        Future电影
      </el-link>
      <sn-row-has-two-col :left="16" :right="8">
        <el-image
            slot="one-col"
            src="http://s0.meituan.net/bs/file/?f=fe-sso-fs:build/page/static/banner/maoyan.png"/>
        <div slot="two-col" class="login-form">
          <sn-row-has-two-col :left="16" :right="8" class="login-method">
            <sn-text text="账号登录" slot="one-col"/>
            <el-link type="danger" :underline="false" slot="two-col" @click="toLoginByPhone">
              手机动态码登录
            </el-link>
          </sn-row-has-two-col>
          <el-form
              :rules="rules"
              ref="userInfo"
              label-position="left"
              label-width="70px"
              :model="userInfo">
            <el-form-item label="手机号" prop="phoneNumber">
              <el-input v-model="userInfo.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="userInfo.password"></el-input>
            </el-form-item>
            <el-button
                @click="login('userInfo')"
                type="danger" style="width: 100%">
              登录
            </el-button>
            <div class="to-register">
              还没有账号?
              <el-link
                  @click="toRegister"
                  type="danger" :underline="false">
                免费注册
              </el-link>
            </div>
          </el-form>
        </div>
      </sn-row-has-two-col>
    </div>
  </sn-three-col-space>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      userInfo: {
        phoneNumber: '',
        password: ''
      },
      rules: {
        phoneNumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    toHome() {
      this.$router.push({name: 'home'})
    },
    toRegister() {
      this.$router.push({name: 'register'})
    },
    toLoginByPhone() {
      this.$router.push({name: 'loginByPhone'})
    },
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http({
            url: '/web/user/login/loginByPassword',
            method: 'post',
            data: this.$http.adornData({
              'phone': this.userInfo.phoneNumber,
              'password': this.userInfo.password
            })
          }).then(({data}) => {
            if (data.code === 0) {
              this.$store.commit(this.mutationsTypes.USER_LOGIN, data.data)
              this.$router.replace({
                name: 'home'
              })
            } else {
              this.myUtils.showMessage(this, data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.login {
  margin-top: 50px;
}

.login-title {
  font-size: 50px;
  color: #ED1D24;
  font-weight: bold;
  margin-bottom: 30px;
}

.login-method {
  margin-bottom: 20px;
}

.to-register {
  margin-top: 20px;
}
</style>